package com.qzdsoft.erpcloud.controller.com;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.com.Department;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.com.DepartmentService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.DepartmentQueryParam;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;

/**
 * 部门管理， yyb
 */
@Controller
@RequestMapping("/com/department")
public class DepartmentController {
	 private static final Logger logger = LoggerFactory.getLogger(DepartmentController.class);
	private static final String BASEPATH = "com/department";
	 @Autowired
    private DepartmentService departmentService;
	  @Autowired
    private CenterCompanyService actCenterCompanyService;

	  
	@Permission(validate=false)
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Model model) {
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
    	model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
       // return "com/department";
    	return BASEPATH+"/index";
    }
    
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse data(HttpServletRequest request, DepartmentQueryParam param) {
        if(param.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
        	 List list = departmentService.selectData(param);
        	 LayTableResponse result =  new LayTableResponse(list);
        	return result;
        }
    }
    
    /**
     * 删除部门
     * @param request
     * @param id
     * @return
     */
    @SysLog(description="部门管理删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
    	logger.debug("删除部门信息:id={}",id);
        return departmentService.del(id);
//    	return Response.success("");
    }
    /**
     * 打开修改页面
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam("id") Integer id) {
 	   logger.debug("部门管理修改页面:id={}",id);
 	  ModelAndView mv = new ModelAndView();
 	  mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
 	  if (null != id) {
 		 Department department = departmentService.selectById(id);
 		 mv.addObject("obj", department);
 	   }
 	  mv.setViewName(BASEPATH+"/edit");
 	  return mv;
    }
    
    /**
     * 打开详情页面
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/view")
    public ModelAndView view(@RequestParam("id") Integer id) {
 	   logger.debug("部门管理修改页面:id={}",id);
 	  ModelAndView mv = new ModelAndView();
 	  mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
 	  if (null != id) {
 		 Department department = departmentService.selectById(id);
 		 mv.addObject("obj", department);
 	   }
 	  mv.setViewName(BASEPATH+"/view");
 	  return mv;
    }
    /**
     * 打开新增页面
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView toAdd() {
 	   logger.debug("部门管理新增页面");
 	  ModelAndView mv = new ModelAndView();
 	  mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
 	  mv.setViewName(BASEPATH+"/add");
 	  return mv;
    }
    
    @SysLog(description="部门管理新增保存")
    @ResponseBody
    @RequestMapping(value = "/add")
    public Response add(Department department ,HttpServletRequest request) {
 	   logger.debug("部门编辑保存:Id={},name={},companyId={}",department.getId(),department.getName(),department.getCompanyId());
//     User loginUser = sessionService.getLoginInfo(request);
// 	   department.setCompanyId(1);
 	   return departmentService.edit(department);
    }
    /**
     * 新增修改保存
     * @param department
     * @param request
     * @return
     */
    @SysLog(description="部门管理修改保存")
    @ResponseBody
    @RequestMapping(value = "/edit_execute")
    public Response editDepartment(Department department ,HttpServletRequest request) {
 	   logger.debug("部门编辑保存:Id={},name={},companyId={}",department.getId(),department.getName(),department.getCompanyId());
//     User loginUser = sessionService.getLoginInfo(request);
// 	   department.setCompanyId(1);
 	   return departmentService.edit(department);
    }

    
    @ResponseBody
    @RequestMapping(value = "/getByCompanyId", method = RequestMethod.POST)
    public Response getByCompanyId(Integer companyId,HttpServletRequest request) {
        return new Response(200,"",departmentService.getByCompanyId(companyId));
    }
    
    
    
    
    
    
    
    
    
    
    
   /* 
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public Object page(HttpServletRequest request, Model model, DepartmentQueryParam params) {
    	//User loginUser = sessionService.getLoginInfo(request);
    	User loginUser = null;
    	params.setCompanyId(1);
        Page<DepartmentInfo> departmentPage = departmentService.findPage(params,loginUser);
        return departmentPage;
//        return null;
    }*/

 /* @RequestMapping(value = "toAddIndex", method = RequestMethod.GET)
    public String toAddIndex(Model model,HttpServletRequest request) {
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
	  model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
        return "com/department/add/index";
    }*/
  /*@ResponseBody
    @RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
    public Response addDepartment(String name,String remark,Integer companyId, HttpServletRequest request) {
        User loginUser = sessionService.getLoginInfo(request);
        return departmentService.addDepartment(name,remark,loginUser.getCompanyId());
    }

    @RequestMapping(value = "getDetail/{departmentId}", method = RequestMethod.GET)
    public String getDetail(@PathVariable("departmentId") Integer departmentId, Model model) {
        model.addAttribute("entity", departmentService.getById(departmentId));
        return "/com/department/detail/index";
    }*/
   /* @RequestMapping(value = "/toEdit/{departmentId}", method = RequestMethod.GET)
    public String toEdit(@PathVariable("departmentId") Integer departmentId,Model model,HttpServletRequest request) {
//        User loginUser = sessionService.getLoginInfo(request);
        model.addAttribute("entity", departmentService.selectById(departmentId));
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
        return "/com/department/edit/index";
    }*/
    


    
}
